package org.third.integration.kubernates.batch.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class K8sYamlUtils {

	public static final String INSTANCE_DELIMITER = "---";
	
	//split yaml by delemeter "---"
	public static List<String> split(InputStream is) throws Exception{
		List<String> yamlContentList = new ArrayList<String>();
		BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
		try {
			String aLine;
			StringBuffer sb = new StringBuffer();
			while ((aLine = in.readLine()) != null) {
				if(!aLine.startsWith(INSTANCE_DELIMITER)){
					sb.append(aLine).append(System.lineSeparator());
				}else{
					yamlContentList.add(sb.toString());
	                sb = new StringBuffer();
				}
			}
			if(sb.toString().length() > 0){
				yamlContentList.add(sb.toString());
			}
		} catch (Exception e) {
			throw e;
		}finally{
				in.close();
		}
		return yamlContentList;
	}
	
}
